<?php
class Category_model extends CI_Model{
	function __construct(){
        // Call the Model constructor
        parent::__construct();
    }
    public function buildOptCatRecursive($aCategory,$iCurrent = 0, $current="0", $res = "", $sep="--"){
		$chk="";
		foreach($aCategory as $key => $row){
			if($row["category_parent"] == $iCurrent){
				if($row["category_id"] == $current){
					$chk = "selected='selected'";
				}
				else{
					$chk = "";
				}
				$str = "<option value='". $row["category_id"] ."'".$chk.">|".$sep." ".$row["category_name"]."</option>";
				$res = $str.$this->buildOptCatRecursive($aCategory,$row["category_id"],$current, $res, $sep."---");
			}
		}
		return $res;
	}
    public function buildListCatRecursive($aCategory,$iCurrent = 0, $current="0", $res = "", $sep=""){
		$chk="";
		$icon = "<i class='icon-folder-close'></i>";
		$activate = "";
		foreach($aCategory as $key => $row){
			if($row["category_parent"] == $iCurrent){
				if($row["category_activate"] > 0){
					$activate = "<i class='icon-eye-open' title='Active'></i>";
				}
				else{
					$activate = "<i class='icon-eye-close' title='Inactive'></i>";
				}
				if($row['category_parent'] > 0){
					$icon = "<i style='margin-left: 15px;' class='icon-folder-close-alt'></i>";
				}
				else{
					$icon = "<i class='icon-folder-close'></i>";
				}
				$str = "<tr onMouseOver='this.style.background=\"lavender\"' onMouseOut='this.style.background=\"\"'><td><input type='checkbox' name='item[]' value='".$row['category_id']."'></td><td><a href='".SITE_URL."/category/edit/".$row["category_id"]."'>".$icon." ".$sep." ".$row["category_name"]."</a></td><td><input class='input-sm form-control' style='width:40px;height:21px;text-align:center;' onClick='this.select()' type='text' name='category_order[".$row["category_id"]."]' value='".$row["category_order"]."' /></td><td>".$activate."</td></tr>";
				$res = $str.$this->buildListCatRecursive($aCategory,$row["category_id"],$current, $res, $sep."----/");
			}
		}
		return $res;
	}
    public function getAllCategory(){
    	try{
    		$query = $this->db->query("CALL backend_getCategoryFull()");
    		$result = $query->result_array();
    		
			return $result;
		}
    	catch(Exception $e){
    		log_message('error', $e->getMessage());
    	}
    }
    public function reorderCategory($arrOrder){
    	foreach($arrOrder as $id => $order){
    		try{
	    		$query = $this->db->query("CALL backend_reorderCategory(".intval($order).", ".intval($id).")");
			}
	    	catch(Exception $e){
	    		log_message('error', $e->getMessage());
	    	}
    	}
    	return true;
    }
    public function deleteCategory($arrOrder){
    	foreach($arrOrder as $id){
    		try{
	    		$query = $this->db->query("CALL backend_deleteCategory(".intval($id).")");
			}
	    	catch(Exception $e){
	    		log_message('error', $e->getMessage());
	    	}
    	}
    	return true;
    }
    public function getDetailCategory($cid){
		$result = array();
		try{
    		$query = $this->db->query("CALL backend_getDetailCategory(".intval($cid).")");
    		$result = $query->result_array();
		}
    	catch(Exception $e){
    		log_message('error', $e->getMessage());
    	}
    	return $result[0];
    }
    public function addCategory($data){
    	$this->load->helper('str');
    	array_walk($data, 'cleanValue');
    	if(!empty($data)){
    		try{
	    		$query = $this->db->query("CALL backend_insertCategory('".$data['category_name']."', ".(int)$data['category_order'].", ".(int)$data['category_activate'].", ".(int)$data['category_parent'].")");
	    		return true;
			}
	    	catch(Exception $e){
	    		log_message('error', $e->getMessage());
	    	}
    	}
    	return false;
    }
}